//
//  APPCenter.m
//  HSY
//
//  Created by 陈浩 on 2017/3/31.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "APPCenter.h"
#import "HSYCheckIsInReviewRequest.h"
#import "SAMKeychain.h"
#import "HSYLoginRequest.h"

@interface APPCenter ()

@property (nonatomic, strong) HSYLoginRequest *task;

@end

@implementation APPCenter

+(instancetype) defaultCenter{
    static APPCenter *app = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!app) {
            app = [[self alloc] init];
        }
    });
    return app;
}

- (void)setIsInReview:(BOOL)isInReview{
    NSString *boolString = isInReview ? @"1" : @"0";
    [[NSUserDefaults standardUserDefaults] setObject:boolString forKey:@"inReview"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

- (BOOL)isInReview{
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"inReview"];
    if ([str isEqualToString:@"1"]) {
        return YES;
    }else if ([str isEqualToString:@"0"]){
        return NO;
    }else{
        return YES;
    }
}


- (void)check{
    if (!self.isInReview) return; //判断为线上用户，不再发送请求
    
    HSYCheckIsInReviewRequest *task = [[HSYCheckIsInReviewRequest alloc] init];
    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            if ([response.encryptDataResult[@"isReview"] isEqualToString:@"1"]) {
                self.isInReview = YES;
            }else{
                self.isInReview = NO;
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationReViewSuccess object:nil];
            }
        }

    } failure:nil];
}

- (void)setPaymentTone:(BOOL)paymentTone{
    [[NSUserDefaults standardUserDefaults] setBool:paymentTone forKey:@"SetPaymentNotWarningTone"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


- (BOOL)paymentTone{
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"SetPaymentNotWarningTone"];
}

- (NSString *)password{
    return [SAMKeychain passwordForService:kHSYKeyChainServerName account:@"password"];
}

- (NSString *)username{
    return [SAMKeychain passwordForService:kHSYKeyChainServerName account:@"username"];
}


- (void)setPassword:(NSString *)password{
    if (!password) {
        [SAMKeychain deletePasswordForService:kHSYKeyChainServerName account:@"password"];
    }else{
        [SAMKeychain setPassword:password forService:kHSYKeyChainServerName account:@"password"];
    }
}

- (void)setUsername:(NSString *)username{
    if (!username) {
        [SAMKeychain deletePasswordForService:kHSYKeyChainServerName account:@"username"];
    }else{
        [SAMKeychain setPassword:username forService:kHSYKeyChainServerName account:@"username"];
    }
}

- (void)tryLoginComplete:(void(^)(NSString *errorString))complete{
    //如果非登录状态，尝试进行登录
    if (self.username.length > 0 && self.password.length > 0) {
        HSYLoginRequest *task = [[HSYLoginRequest alloc] init];
        task.cellphone = self.username;
        task.password = self.password;
        self.task =task;
        [HSYNetworkManager POST:task success:^(HSYResponse *response) {
            if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
                //获取用户信息并储存
                HSYUserCenter *userCenter = [HSYUserCenter mj_objectWithKeyValues:response.encryptDataResult];
                [userCenter saveUserModelToDocuments];
                
            }
        } failure:nil];
    }
}

- (void)cancelLoginIfNeed{
    if (self.task) {
        [self.task cancelCurrentTask];
    }
}

@end
